//
//  NewFeatureCollectionViewController.swift
//  DSWeibo
//
//  Created by J on 16/4/17.
//  Copyright © 2016年 J. All rights reserved.
//

import UIKit

class NewFeatureCollectionViewController: UICollectionViewController {

    private let pageCount = 4
    private let layout:NewFeatureFlowLayout = NewFeatureFlowLayout()
    private let cellReuseIdentifier = "cellReuseIdentifier"
    
    init(){
        super.init(collectionViewLayout: layout)
        
        collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true

        
    }

    
    
}

extension NewFeatureCollectionViewController{
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pageCount
    }
    
    
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellReuseIdentifier, forIndexPath: indexPath) as!NewFeatureCell
        cell.backgroundColor = UIColor.orangeColor()
        cell.imageIndex = indexPath.item
        return cell

        
    }
    
    
    
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let path = collectionView.indexPathsForVisibleItems().last!
        let cell = collectionView.cellForItemAtIndexPath(path) as!NewFeatureCell
        if path.item == pageCount-1 {
            cell.startAnimation()
        }
        
    }
    
    
}


 class NewFeatureCell: UICollectionViewCell{
    
    private lazy var iconView = UIImageView()
    private var imageIndex: Int?{
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            startButton.hidden = true
        }
 
    }
    
    
    override init(frame: CGRect) {
       super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func setUpUI(){
        contentView.addSubview(iconView)
        iconView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        contentView.addSubview(startButton)
        startButton.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-100)
        }
        
    }
    
    
    private lazy var startButton:UIButton = {
        let btn = UIButton()
        btn.hidden = true
        btn.setBackgroundImage(UIImage(named:"new_feature_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: #selector(NewFeatureCell.click), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    func click(){
        NSNotificationCenter.defaultCenter() .postNotificationName(XMGSwitchRootViewControllerKey, object: true)
        
    }
    
    func startAnimation(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0.0, 0.0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 15, options: UIViewAnimationOptions(rawValue:0), animations: { 
            self.startButton.transform = CGAffineTransformIdentity
            self.startButton.userInteractionEnabled = true
            }) { (Bool) in
                
        }
        
        
    }

}


private class NewFeatureFlowLayout:UICollectionViewFlowLayout{
    
    
    private override func prepareLayout() {
        itemSize = UIScreen.mainScreen().bounds.size
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
    }
    
    
}
